<?php
/*
Copyright 2008, Nathalia


Author:
Nathalia Sautchuk Patrício          (nathalia.sautchuk@gmail.com)

*/
class Editar extends TPage
{
	public function onInit($param)
	{
		parent::onInit($param);

        $this->CPList->DataTextField='Nome';
		$this->CPList->DataValueField='UsuarioID';
        $criteria = new TActiveRecordCriteria;
        $criteria->Condition = "Papel = 2";
		$this->CPList->DataSource=UserRecord::finder()->findAll($criteria);
		$this->CPList->dataBind();

		if(!$this->IsPostBack)  // if the page is initially requested
		{
            $congressoRecord                  = $this->CongressoRecord;
            $this->DataInicio->Text           = $congressoRecord->DataInicio;          
            $this->DataTermino->Text          = $congressoRecord->DataInicio;          
            $this->DataSubmissaoInicio->Text  = $congressoRecord->DataSubmissaoInicio;
            $this->DataSubmissaoTermino->Text = $congressoRecord->DataSubmissaoTermino;
            $this->Nome->Text                 = $congressoRecord->Nome;
            $this->Descricao->Text            = $congressoRecord->Descricao;
            $this->Local->Text                = $congressoRecord->Local;        
            $this->AreaConhecimento->Text     = $congressoRecord->AreaConhecimento; 
            $this->CPList->setSelectedValue($congressoRecord->CpID);             
		}
        
	}

	public function editarCongresso($sender,$param)
	{
		if($this->IsValid)  // when all validations succeed
		{
            $congressoRecord                       = $this->CongressoRecord;
            $congressoRecord->DataInicio           = $this->DataInicio->Text; 
            $congressoRecord->DataTermino          = $this->DataTermino->Text; 
            $congressoRecord->DataSubmissaoInicio  = $this->DataSubmissaoInicio->Text; 
            $congressoRecord->DataSubmissaoTermino = $this->DataSubmissaoTermino->Text; 
            $congressoRecord->Nome                 = $this->Nome->Text;
            $congressoRecord->Descricao            = $this->Descricao->Text;
            $congressoRecord->Local                = $this->Local->Text;
            $congressoRecord->AreaConhecimento     = $this->AreaConhecimento->Text;
            $congressoRecord->CpID                 = $this->CPList->getSelectedValue();        

			// saves to the database via Active Record mechanism
			$congressoRecord->save();

			// redirects the browser to the homepage
            $this->Response->redirect('./index.php?page=Congressos.Listar');
		}
	}

	protected function getCongressoRecord()
	{
		// if the 'username' GET var is not empty and the current user
		// is an administrator, we use the GET var value instead.
		if($this->Request['congressoID']!==null)
			$congressoID=$this->Request['congressoID'];

		// use Active Record to look for the specified username
		$congressoRecord=CongressoRecord::finder()->findByCongressoID($congressoID);
		if(!($congressoRecord instanceof CongressoRecord))
			throw new THttpException(500,'CongressoID is invalid.');
		return $congressoRecord;
	}


}
?>
